

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=dark>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/my-blog/img/fluid.png">
  <link rel="icon" href="/my-blog/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="木舟">
  <meta name="keywords" content="python Vue Test 测试开发 自动化测试">
  
    <meta name="description" content="判断判断是每个程序中必不可少的部分，它能够控制程序的走向，它甚至能够决定一个项目、一段代码的好坏。 可以说，运用好判断，不仅能够让你少写代码，也能够让你的程序更加健壮，甚至可以让你的程序运行的速度更快。 在任何语言中，判断条件 的成立，规则都是一样的，即 判断的表达式为真 所以，我们下面会根据 如何区别一个表达式是否为真来进行教学。 缩进：在python中，缩进很重要，因为缩进能够决定某段代码处于">
<meta property="og:type" content="article">
<meta property="og:title" content="python判断与循环">
<meta property="og:url" content="https://walker_kwu.gitee.io/my-blog/2023/03/04/python/base/python%E5%88%A4%E6%96%AD%E4%B8%8E%E5%BE%AA%E7%8E%AF/">
<meta property="og:site_name" content="My Blog">
<meta property="og:description" content="判断判断是每个程序中必不可少的部分，它能够控制程序的走向，它甚至能够决定一个项目、一段代码的好坏。 可以说，运用好判断，不仅能够让你少写代码，也能够让你的程序更加健壮，甚至可以让你的程序运行的速度更快。 在任何语言中，判断条件 的成立，规则都是一样的，即 判断的表达式为真 所以，我们下面会根据 如何区别一个表达式是否为真来进行教学。 缩进：在python中，缩进很重要，因为缩进能够决定某段代码处于">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-03-04T02:56:15.000Z">
<meta property="article:modified_time" content="2023-05-23T10:37:53.264Z">
<meta property="article:author" content="木舟">
<meta property="article:tag" content="测试开发">
<meta property="article:tag" content="Python基础">
<meta name="twitter:card" content="summary_large_image">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>python判断与循环 - My Blog</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/my-blog/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/my-blog/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/my-blog/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"walker_kwu.gitee.io","root":"/my-blog/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"left","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/my-blog/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/my-blog/js/utils.js" ></script>
  <script  src="/my-blog/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/my-blog/">
      <strong>木舟</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/my-blog/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="python判断与循环"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        木舟
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2023-03-04 10:56" pubdate>
          2023年3月4日 上午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          4.4k 字
        
      </span>
    

    

    
    
      
        <span id="busuanzi_container_page_pv" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="busuanzi_value_page_pv"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="padding-left: 2rem; margin-right: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">python判断与循环</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：2023年5月23日 晚上
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <h1 id="判断"><a href="#判断" class="headerlink" title="判断"></a>判断</h1><p>判断是每个程序中必不可少的部分，它能够控制程序的走向，它甚至能够决定一个项目、一段代码的好坏。</p>
<p>可以说，运用好判断，不仅能够让你少写代码，也能够让你的程序更加健壮，甚至可以让你的程序运行的速度更快。</p>
<p>在任何语言中，<strong>判断条件</strong> 的成立，规则都是一样的，即 <strong>判断的表达式为真</strong></p>
<p>所以，我们下面会根据 如何区别一个<strong>表达式是否为真</strong>来进行教学。</p>
<p><strong>缩进</strong>：在python中，缩进很重要，因为缩进能够决定某段代码处于哪个判断逻辑/循环逻辑下。</p>
<p>和其他语言不同，如：JavaScript，因为花括号的存在，JavaScript不需要缩进也能够正常执行代码。</p>
<p>而python没有花括号，一是为了简洁，二是为了强制规范。</p>
<h2 id="判断写法"><a href="#判断写法" class="headerlink" title="判断写法"></a>判断写法</h2><h3 id="单独判断"><a href="#单独判断" class="headerlink" title="单独判断"></a>单独判断</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs python">a = <span class="hljs-number">1</span><br><span class="hljs-keyword">if</span> a:<br>    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;判断通过&quot;</span>)<br><br><span class="hljs-comment"># 判断可以单独只写一个判断，判断通过时，运行相对应的程序</span><br><span class="hljs-comment"># 注意，这里代码是有 “缩进” 行为的</span><br></code></pre></td></tr></table></figure>

<h3 id="else"><a href="#else" class="headerlink" title="else"></a>else</h3><p>既然有判断通过能够执行的代码，那么肯定也有判断不通过能够执行的代码。</p>
<p>归属于同一判断条件，我们使用 <strong>else</strong> 来进行区分</p>
<p>和正常的思维逻辑一样，如果判断通过，那么else就不会被触发了</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs python">a = <span class="hljs-number">0</span><br><span class="hljs-keyword">if</span> a:<br>    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;判断通过&quot;</span>)<br><span class="hljs-keyword">else</span>:<br>    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;判断不通过&quot;</span>)<br></code></pre></td></tr></table></figure>

<h3 id="多分支判断"><a href="#多分支判断" class="headerlink" title="多分支判断"></a>多分支判断</h3><p>多分支判断在任何编程语言中都存在，唯一有区别的就是多分支的关键字，python为 <strong>elif</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs python">a = <span class="hljs-number">2</span><br><span class="hljs-keyword">if</span> a &gt; <span class="hljs-number">3</span>:<br>    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;判断通过&quot;</span>)<br><span class="hljs-keyword">elif</span> a &lt; <span class="hljs-number">1</span>:<br>    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;判断通过&quot;</span>)<br><span class="hljs-keyword">elif</span> a &gt; <span class="hljs-number">1</span>: <span class="hljs-comment"># 触发</span><br>    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;判断通过&quot;</span>)<br><span class="hljs-keyword">elif</span> a &lt; <span class="hljs-number">0</span>: <span class="hljs-comment"># 不会判断了</span><br>    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;判断通过&quot;</span>)<br><span class="hljs-keyword">else</span>:<span class="hljs-comment"># 不会触发了</span><br>    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;判断不通过&quot;</span>)<br></code></pre></td></tr></table></figure>

<p>多分支判断有个特点，就是只要分支中任一一个分支判断通过时，以下的所有分支以及else都不会触发。</p>
<h3 id="嵌套判断"><a href="#嵌套判断" class="headerlink" title="嵌套判断"></a>嵌套判断</h3><p>程序的运行，往往是很复杂的，所以面对复杂的情况，我们通常会使用嵌套判断的方式来应对。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs python">lst = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>]<br><br><span class="hljs-keyword">if</span> lst:<br>    <span class="hljs-keyword">if</span> <span class="hljs-number">5</span> <span class="hljs-keyword">in</span> lst:<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;5不在列表中&quot;</span>)<br>    <span class="hljs-keyword">else</span>:<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;5在列表中&quot;</span>)<br><span class="hljs-keyword">else</span>:<br>    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;列表为空，不需要进行判断&quot;</span>)<br></code></pre></td></tr></table></figure>

<h3 id="为什么判断会让你的程序运行更快"><a href="#为什么判断会让你的程序运行更快" class="headerlink" title="为什么判断会让你的程序运行更快"></a>为什么判断会让你的程序运行更快</h3><p>我们回到判断的规则，<strong>判读通过即运行相对应的代码</strong></p>
<p>那么反过来理解，<strong>判断不通过则不会运行相对应的代码</strong></p>
<p>我们在编写程序时，必不可少的会编写到一些耗时的代码，这个时候我们就可以利用判断的机制，巧妙的绕过它们，只在需要它们执行的时候再去执行。</p>
<p>当然这部分涉及到一些其他的知识，无法很好的给大家展示，下面就简单举个例子。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs python">a = <span class="hljs-number">123</span><br>b = <span class="hljs-number">124</span><br><br><span class="hljs-keyword">if</span> b - a &gt; <span class="hljs-number">0.5</span>:<br>    <span class="hljs-comment"># 一些耗时的计算</span><br>    ...<br><span class="hljs-keyword">else</span>:<br>    <span class="hljs-comment"># 符合条件的a不需要计算了</span><br>    ...<br></code></pre></td></tr></table></figure>

<p>想象一下，我们正在编写很耗时的科学计算程序，目的是为了算出两个值的误差最终小于 <strong>0.5</strong></p>
<p>那么如果一开始我们得到的值就是符合条件的，那么也就不需要进行这一项耗时的计算了。</p>
<h2 id="什么才为真？"><a href="#什么才为真？" class="headerlink" title="什么才为真？"></a>什么才为真？</h2><p>我们开篇说过，判断通过的依据是：<strong>表达式为真</strong></p>
<p>那么怎么区分一个表达式为真？</p>
<p>我给大家总结了一下几个点：</p>
<ul>
<li>不为0的数字</li>
<li>不为空的字符串</li>
<li>不为空的数组</li>
<li>不为空的字典</li>
<li>True</li>
<li>不为None</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs python">a = -<span class="hljs-number">1</span><br>b = <span class="hljs-number">0.1</span><br>c = <span class="hljs-number">0</span><br><br><span class="hljs-keyword">if</span> a:<br>    ...<br><span class="hljs-keyword">if</span> b:<br>    ...<br><span class="hljs-keyword">if</span> c: <span class="hljs-comment"># 不通过</span><br>    ...<br></code></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs python">a = <span class="hljs-string">&#x27;a&#x27;</span><br>b = <span class="hljs-string">&#x27; &#x27;</span><br>c = <span class="hljs-string">&#x27;&#x27;</span><br><br><span class="hljs-keyword">if</span> a:<br>    ...<br><span class="hljs-keyword">if</span> b:<br>    ...<br><span class="hljs-keyword">if</span> c: <span class="hljs-comment"># 不通过</span><br>    ...<br></code></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs python">a = [<span class="hljs-number">0</span>]<br>b = [<span class="hljs-string">&#x27;&#x27;</span>]<br>c = []<br><br><span class="hljs-keyword">if</span> a:<br>    ...<br><span class="hljs-keyword">if</span> b:<br>    ...<br><span class="hljs-keyword">if</span> c: <span class="hljs-comment"># 不通过</span><br>    ...<br></code></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs python">a = &#123;<span class="hljs-string">&#x27;w&#x27;</span>: <span class="hljs-number">0</span>&#125;<br>b = &#123;<span class="hljs-string">&#x27;w&#x27;</span>: []&#125;<br>c = []<br><br><span class="hljs-keyword">if</span> a:<br>    ...<br><span class="hljs-keyword">if</span> b:<br>    ...<br><span class="hljs-keyword">if</span> c: <span class="hljs-comment"># 不通过</span><br>    ...<br></code></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs python">a = <span class="hljs-literal">True</span><br>c = <span class="hljs-literal">None</span><br><br><span class="hljs-keyword">if</span> a:<br>    ...<br><span class="hljs-keyword">if</span> c: <span class="hljs-comment"># 不通过</span><br>    ...<br></code></pre></td></tr></table></figure>

<h3 id="表达式"><a href="#表达式" class="headerlink" title="表达式"></a>表达式</h3><p>可能会有疑问，哎，不是说什么 <strong>表达式</strong> 吗？怎么举例的都是 <strong>数据类型</strong></p>
<p>首先，我们要明确什么叫 <strong>表达式</strong></p>
<p>表达式指的是：能够产生对应值的简短代码</p>
<p>比如：一个函数、一个运算、一个数据类型 这些都可以叫做表达式，因为它们都会产生对应的值</p>
<h2 id="判断与运算符"><a href="#判断与运算符" class="headerlink" title="判断与运算符"></a>判断与运算符</h2><p>我们在 <strong>python基础（上）</strong> 里有介绍过各种各样的运算符</p>
<p>其中的 <strong>比较运算符、逻辑运算符</strong> 一般就可以拿来与判断联用</p>
<p>因为这两种运算符，最后都会产生 <strong>True/False</strong>，但是这个说法其实并不严谨，<strong>逻辑运算符</strong>并不会只产生 <strong>True/False</strong>，这个我们后续再做介绍。</p>
<p>恰好的是，我们的判断，就需要 <strong>True/False</strong> 来进行判断</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs python">a = <span class="hljs-number">1</span><br><span class="hljs-keyword">if</span> a == <span class="hljs-number">1</span>:<br>    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;判断通过&quot;</span>)<br><br>b = <span class="hljs-number">2</span><br><br><span class="hljs-keyword">if</span> a == <span class="hljs-number">1</span> <span class="hljs-keyword">and</span> b == <span class="hljs-number">2</span>:<br>    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;判断通过&quot;</span>)<br></code></pre></td></tr></table></figure>

<h2 id="三元运算"><a href="#三元运算" class="headerlink" title="三元运算"></a>三元运算</h2><p>也叫做 <strong>三目运算</strong></p>
<p>将多层的结构变为一层，先对【条件】进行判断，【条件成立】返回值1；否则返回值2</p>
<p>三元运算，一般是用来单独设置一个值</p>
<p>由于只是为了赋值，并没有复杂的代码段，所以就有了三元运算</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs python">lst = [<span class="hljs-number">1</span>]<br><br><span class="hljs-keyword">if</span> <span class="hljs-built_in">len</span>(lst):<br>    a = lst[<span class="hljs-number">0</span>]<br><span class="hljs-keyword">else</span>:<br>    a = <span class="hljs-number">1</span><br><br>b = lst[<span class="hljs-number">0</span>] <span class="hljs-keyword">if</span> <span class="hljs-built_in">len</span>(lst) <span class="hljs-keyword">else</span> <span class="hljs-number">1</span><br></code></pre></td></tr></table></figure>

<p>上述代码的作用是：如果列表不为空，则取列表第一个值，如果为空，则给他一个默认值</p>
<h1 id="循环"><a href="#循环" class="headerlink" title="循环"></a>循环</h1><p>和 <strong>判断</strong> 一样，循环也是控制程序运行的一部分手段</p>
<p>如果说 <strong>判断</strong> 是控制程序的走向，那么循环就是用来让程序完成某种比较高级的功能，比如：自动化统计</p>
<p>循环执行的代码块同样需要 <strong>缩进</strong></p>
<p>循环分为两种：</p>
<ul>
<li>for</li>
<li>while</li>
</ul>
<p>下面我们就根据这两种循环来依次介绍</p>
<h2 id="for"><a href="#for" class="headerlink" title="for"></a>for</h2><h3 id="基本写法"><a href="#基本写法" class="headerlink" title="基本写法"></a>基本写法</h3><p><strong>range</strong> range会创建一个有序的序列，该序列可以用来循环</p>
<p><strong>range + for</strong>一般是用来计数循环</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">for</span> loop <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">10</span>):<br>    <span class="hljs-built_in">print</span>(loop)<br><br><span class="hljs-comment"># 结果：0，1，2，3，4，5，6，7，8，9</span><br></code></pre></td></tr></table></figure>

<p>首先我们来看一下range函数会生产出什么。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-built_in">print</span>(<span class="hljs-built_in">range</span>(<span class="hljs-number">10</span>)) <span class="hljs-comment"># range实例对象</span><br><span class="hljs-built_in">print</span>(<span class="hljs-built_in">list</span>(<span class="hljs-built_in">range</span>(<span class="hljs-number">10</span>))) <span class="hljs-comment"># 我们可以通过list将其转化为列表 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]</span><br></code></pre></td></tr></table></figure>

<p>不难看出，range可以接收一个参数，这个参数可以用来规定range最终产出的序列大小。</p>
<p>起始数为0，最终数为传入参数的数字-1</p>
<p>但range实际上可以接受三个参数，其他两个都是 <strong>默认参数</strong>（什么是默认参数，我们会在介绍 <strong>函数</strong> 的时候给大家讲解）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-built_in">range</span>(start, stop, step)<br></code></pre></td></tr></table></figure>

<p>start: 计数从 start 开始。默认是从 0 开始。range(4) 相当于 range(0, 4)<br>stop: 计数到 stop 结束，不包括 stop。range(0,4) 表达 [0, 1, 2, 3]<br>step：步长，默认为1。range(0, 3) 相当于 range(0, 3, 1)</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-built_in">range</span>(<span class="hljs-number">0</span>, <span class="hljs-number">10</span>, <span class="hljs-number">2</span>) <span class="hljs-comment"># -&gt; 步长为2，表示每两步输出一个数</span><br><br><span class="hljs-comment"># 结果 [0, 2, 4, 6, 8]</span><br></code></pre></td></tr></table></figure>

<h3 id="多参数循环"><a href="#多参数循环" class="headerlink" title="多参数循环"></a>多参数循环</h3><p>for循环可以是多个参数变量来接住循环出来的元素</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs python">lst = [<br>    [<span class="hljs-number">0</span>, <span class="hljs-number">1</span>],<br>    [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>],<br>    [<span class="hljs-number">4</span>, <span class="hljs-number">5</span>]<br>]<br><br><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> lst:<br>    <span class="hljs-built_in">print</span>(i)<br><br><span class="hljs-comment"># 结果</span><br><span class="hljs-comment"># [0, 1]</span><br><span class="hljs-comment"># [1, 2]</span><br><span class="hljs-comment"># [4, 5]</span><br><br><span class="hljs-keyword">for</span> i, p <span class="hljs-keyword">in</span> lst:<br>    <span class="hljs-built_in">print</span>(i)<br>    <span class="hljs-built_in">print</span>(p)<br><br><span class="hljs-comment"># 0</span><br><span class="hljs-comment"># 1</span><br><span class="hljs-comment"># 1</span><br><span class="hljs-comment"># 2</span><br><span class="hljs-comment"># 4</span><br><span class="hljs-comment"># 5</span><br></code></pre></td></tr></table></figure>

<p>可以看到，借助多参数，我们可以将 列表中的元素进行 <strong>解包</strong></p>
<p>什么是 <strong>解包</strong>？</p>
<p>解包可以看作是拆分赋值，可以看下面的例子</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs python">a, b = [<span class="hljs-number">0</span>, <span class="hljs-number">1</span>]<br>a, b, c = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]<br></code></pre></td></tr></table></figure>

<h3 id="如何循环一个列表-元组"><a href="#如何循环一个列表-元组" class="headerlink" title="如何循环一个列表/元组"></a>如何循环一个列表/元组</h3><p>循环列表/元组有多种方式</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment"># 循环列表/元素的元素</span><br>lst = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>]<br><span class="hljs-keyword">for</span> el <span class="hljs-keyword">in</span> [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>]:<br>    <span class="hljs-built_in">print</span>(el)<br><br><span class="hljs-keyword">for</span> idx <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-built_in">len</span>(lst)):<br>    <span class="hljs-built_in">print</span>(lst[idx])<br><br><span class="hljs-comment"># 通过循环出下标以及元素，需要借助enumerate</span><br><span class="hljs-keyword">for</span> idx, el <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(lst):  <span class="hljs-comment"># 借助多参数循环的思想，不难看出enumerate产出的是((下标1, 元素1), (下标2, 元素2))</span><br>    <span class="hljs-built_in">print</span>(idx, el)<br></code></pre></td></tr></table></figure>

<h2 id="while"><a href="#while" class="headerlink" title="while"></a>while</h2><p>相比for循环，while循环就要简洁的多，它是基于 <strong>表达式是否为真</strong> 来运行循环</p>
<p>每次循环都会去判断这个表达式是否为真</p>
<p>所以在同等情况下，while循环会比for循环要 <strong>慢</strong></p>
<h3 id="基本写法-1"><a href="#基本写法-1" class="headerlink" title="基本写法"></a>基本写法</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs python">a = <span class="hljs-number">1</span><br><span class="hljs-keyword">while</span> a &gt; <span class="hljs-number">0</span>:<br>    <span class="hljs-built_in">print</span>(a)<br>    a += <span class="hljs-number">1</span><br><br><span class="hljs-comment"># 结果，这个循环会变成死循环，因为每次循环都+1，导致a永远都不可能小于0，表达式永远为真</span><br></code></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs python">a = <span class="hljs-number">10</span><br><span class="hljs-keyword">while</span> a &gt; <span class="hljs-number">0</span>:<br>    <span class="hljs-built_in">print</span>(a)<br>    a -= <span class="hljs-number">1</span><br><br><span class="hljs-comment"># 结果，会输出10到1</span><br></code></pre></td></tr></table></figure>

<h2 id="停止循环"><a href="#停止循环" class="headerlink" title="停止循环"></a>停止循环</h2><p>可以发现，不管是for还是while，都会在达到一定条件后才会自动停止循环</p>
<p>但是有些时候，我们的程序是需要手动去停止循环的。</p>
<p>比如说我计算一个结果，当我达到我的预期的时候，我就可以停止计算</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs python">a = <span class="hljs-number">10</span><br><br><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">10000</span>):<br>    a *= i<br>    <span class="hljs-keyword">if</span> a &gt; <span class="hljs-number">100</span>:<br>        <span class="hljs-comment"># 这个时候我想结束循环</span><br>        ...<br></code></pre></td></tr></table></figure>

<p>所以不管是什么语言，都会有停止循环的关键字</p>
<h3 id="continue"><a href="#continue" class="headerlink" title="continue"></a>continue</h3><p>首先要介绍的是这个关键字，这个关键字严格来说并不是停止循环</p>
<p>而是 <strong>跳过</strong> 某次循环, 当其触发时，处于其下的代码，不论层级缩进，都不会执行</p>
<p>比如我们可以利用这个机制来输出 <strong>奇数</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">10</span>):<br>    <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> i % <span class="hljs-number">2</span>:<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;我是偶数<span class="hljs-subst">&#123;i&#125;</span>，我不要输出&quot;</span>)<br>        <span class="hljs-keyword">continue</span><br>    <span class="hljs-built_in">print</span>(i)<br></code></pre></td></tr></table></figure>

<h3 id="break"><a href="#break" class="headerlink" title="break"></a>break</h3><p>这个关键是会 <strong>终止循环</strong></p>
<p>也就是说，一旦触发，整个循环都会停止，当其触发时，处于其下的代码，不论层级缩进，都不会执行</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs python">a = <span class="hljs-number">1</span><br><span class="hljs-keyword">while</span> <span class="hljs-literal">True</span>:<br>    a += <span class="hljs-number">1</span><br>    <span class="hljs-keyword">if</span> a &gt; <span class="hljs-number">10</span>:<br>        <span class="hljs-keyword">break</span><br>    <span class="hljs-built_in">print</span>(a)<br></code></pre></td></tr></table></figure>

<h2 id="循环也能else"><a href="#循环也能else" class="headerlink" title="循环也能else"></a>循环也能else</h2><p>what？else不是用来判断的吗？</p>
<p>没错，所以他也能用来判断循环是否 <strong>正常结束</strong></p>
<p>这可是python独有</p>
<p>那么什么是 <strong>正常结束</strong>，也就是 循环不是因为触发了 <strong>break</strong> 而结束</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs python">a = <span class="hljs-number">1</span><br><span class="hljs-keyword">while</span> a &lt; <span class="hljs-number">3</span>:<br>    a += <span class="hljs-number">1</span><br>    <span class="hljs-built_in">print</span>(a)<br><span class="hljs-keyword">else</span>:<br>    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;正常结束&quot;</span>)<br><br>a = <span class="hljs-number">1</span><br><span class="hljs-keyword">while</span> a &lt; <span class="hljs-number">10</span>:<br>    a += <span class="hljs-number">1</span><br>    <span class="hljs-keyword">if</span> a &gt; <span class="hljs-number">2</span>:<br>        <span class="hljs-keyword">break</span><br>    <span class="hljs-built_in">print</span>(a)<br><span class="hljs-keyword">else</span>:<br>    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;正常结束&quot;</span>)<br></code></pre></td></tr></table></figure>

<p>同样的，for循环的else也是遵循这个规则</p>
<h2 id="嵌套循环"><a href="#嵌套循环" class="headerlink" title="嵌套循环"></a>嵌套循环</h2><p>循环和判断一样，一样可以嵌套</p>
<p>记住一点就行，只要是有缩进的地方，基本都可以嵌套</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs python">lst = [<br>    [<span class="hljs-number">0</span>, <span class="hljs-number">1</span>],<br>    [<span class="hljs-number">2</span>, <span class="hljs-number">3</span>],<br>    [<span class="hljs-number">4</span>, <span class="hljs-number">5</span>]<br>]<br><br><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> lst:<br>    <span class="hljs-keyword">for</span> p <span class="hljs-keyword">in</span> i:<br>        <span class="hljs-built_in">print</span>(p)<br></code></pre></td></tr></table></figure>
                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/my-blog/categories/python/" class="category-chain-item">python</a>
  
  
    <span>></span>
    
  <a href="/my-blog/categories/python/Python%E5%9F%BA%E7%A1%80/" class="category-chain-item">Python基础</a>
  
  

  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/my-blog/tags/%E6%B5%8B%E8%AF%95%E5%BC%80%E5%8F%91/">#测试开发</a>
      
        <a href="/my-blog/tags/Python%E5%9F%BA%E7%A1%80/">#Python基础</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>python判断与循环</div>
      <div>https://walker_kwu.gitee.io/my-blog/2023/03/04/python/base/python判断与循环/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>木舟</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2023年3月4日</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>更新于</div>
          <div>2023年5月23日</div>
        </div>
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
              <a target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
              <span class="hint--top hint--rounded" aria-label="NC - 非商业性使用">
                <i class="iconfont icon-nc"></i>
              </span>
              </a>
            
              <a target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
              <span class="hint--top hint--rounded" aria-label="SA - 相同方式共享">
                <i class="iconfont icon-sa"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/my-blog/2023/03/05/python/base/python%E5%87%BD%E6%95%B0/" title="python函数">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">python函数</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/my-blog/2023/02/19/python/base/python%E5%9F%BA%E7%A1%80%EF%BC%88%E4%B8%8B%EF%BC%89/" title="python基础（下）">
                        <span class="hidden-mobile">python基础（下）</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <span>By 木舟</span> <i class="iconfont icon-love"></i> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/my-blog/js/events.js" ></script>
<script  src="/my-blog/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/my-blog/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/my-blog/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/my-blog/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
